﻿


Imports System.Data.OleDb

Public Class Form1
    ' Public title As String = ""
    Dim dcol() As Integer = {(0), (0), (0), (0), (0), (0), (0), (0), (0), (0), (0)}
    Dim flag As Boolean = False
    Dim isOpen As Boolean = False
    Public action As String = ""
    Public Did As Integer



    Public Sub actions()
        flag = False
        TabControl1.Visible = True

        Dim strSQL2 As String = "SELECT * FROM Layer WHERE TemplateId =" & Me.Form1TempText.Text & " OR TemplateId =0"

        Dim layernumber As Integer
        Dim html As String
        Dim content As String = ""

        Dim text As HtmlDocument = WebBrowser2.Document
        html = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN''http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>"
        html += "<html xmlns='http://www.w3.org/1999/xhtml'><head><meta content='text/html; charset=utf-8' http-equiv='Content-Type' /><title id='toptitle'></title></head><body >"
        html += "<div style='position: absolute; width: 100%; height: 100%; z-index: 1' id='Main'><h1><center><div style=' position: absolute; top:0px; left:40%; width: 300px; height: 20%;  overflow-x: hidden;	word-wrap: break-word ; z-index: 1' id='9'></div></center></h1><div style=' position: absolute; width: 100%; top:25%; height:75%; z-index: 1; left: 0px;' id='Core'>"
       
        Try
            Using adapter2 As New OleDbDataAdapter(strSQL2, My.Settings.DigitalDocumentConnectionString)


                Dim ds2 As New DataSet
                adapter2.Fill(ds2, "Layer")


                Me.LayerComboBox.DataSource = ds2.Tables("Layer")
                Me.LayerComboBox.ValueMember = "Lid"
                Me.LayerComboBox.DisplayMember = "Type"


            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
     

        Dim strSQL As String = "SELECT count(*) as cc FROM Layer WHERE TemplateId =" & Me.Form1TempText.Text
        Dim strSQL1 As String = "SELECT *  FROM Layer WHERE TemplateId =" & Me.Form1TempText.Text

        Try
            Using cnn As New OleDbConnection(My.Settings.DigitalDocumentConnectionString)
                Using cmd As New OleDbCommand(strSQL, cnn)
                    cnn.Open()

                    Using dr As OleDbDataReader = cmd.ExecuteReader()


                        While dr.Read
                            layernumber = dr("cc")
                        End While
                        cnn.Close()

                    End Using
                End Using
            End Using

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            html = "Problem"
        End Try


        If action = "New" Then
            MsgBox("New")
            isOpen = False


            Try
                Using cnn As New OleDbConnection(My.Settings.DigitalDocumentConnectionString)

                    Dim layer(0 To layernumber) As String
                    Dim counter As Integer = 0

                    Using cmd As New OleDbCommand(strSQL1, cnn)
                        cnn.Open()

                        Using dr As OleDbDataReader = cmd.ExecuteReader()

                            Me.ContentTableAdapter1.Insert(Me.Did, 9, "", "")
                            While dr.Read
                                layer(counter) = "<div id='" + CStr(dr("Lid")) + "' style='background-color:gray; position:" + dr("Position") + "; top:" + dr("PossitionY") + "; left:" + dr("PossitionX") + "; width:" + dr("DissmentionX") + ";  " + dr("InnerLayer") + "'></div>" '<input type='hidden' id='flag' value='0'>
                                Me.ContentTableAdapter1.Insert(Me.Did, dr("Lid"), "", "")
                                counter = counter + 1
                            End While
                        End Using
                    End Using

                    Dim intCount As Integer

                    For intCount = 0 To layernumber
                        content += layer(intCount)

                    Next intCount



                End Using
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                html = "Problem"
            End Try



        ElseIf action = "Open" Then
            isOpen = True

            Try
                Using cnn As New OleDbConnection(My.Settings.DigitalDocumentConnectionString)

                    Dim layer(0 To layernumber) As String
                    Dim counter As Integer = 0

                    Using cmd As New OleDbCommand(strSQL1, cnn)
                        cnn.Open()

                        Using dr As OleDbDataReader = cmd.ExecuteReader()


                            While dr.Read
                                layer(counter) = "<div id='" + CStr(dr("Lid")) + "' style='background-color:gray; position:" + dr("Position") + "; top:" + dr("PossitionY") + "; left:" + dr("PossitionX") + "; width:" + dr("DissmentionX") + "; " + dr("InnerLayer") + "' ></div>"
                                counter = counter + 1
                            End While
                        End Using
                    End Using

                    Dim intCount As Integer

                    For intCount = 0 To layernumber
                        content += layer(intCount)

                    Next intCount



                End Using
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                html = "Problem"
            End Try
        End If


        html += content
        html += "</div></div></body></html>"


        WebBrowser2.DocumentText = html

       

        flag = True


    End Sub
Private Sub NewToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem1.Click
    Form2.Show()
End Sub



    Private Sub ImportToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImportToolStripMenuItem.Click
        OpenDocument.Show()
    End Sub



    'optional
    Private Sub HiddenTemp_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim strSQL1 As String = "SELECT Name FROM Template WHERE TId =" & Me.Form1TempText.Text


        ' Try
        'Using adapter1 As New OleDbDataAdapter(strSQL1, My.Settings.DigitalDocumentConnectionString)


        'Dim ds1 As New DataSet
        ' adapter1.Fill(ds1, "Template")




        ' Me.Form1TempText.Text = ds1.Tables("Template").Rows(0)(0).ToString()



        ' End Using
        'Catch ex As Exception
        'MessageBox.Show(ex.Message)
        'End Try

    End Sub




    Private Sub NewToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem2.Click
        Template.Show()
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TabControl1.Visible = False
        'MsgBox("again")


        'If (Me.Form1TempText.Text <> "") Then

        'Dim strSQL3 As String = "SELECT Context FROM Layer WHERE TemplateId =" & Me.Form1TempText.Text

        'Try
        'Using adapter1 As New OleDbDataAdapter(strSQL3, My.Settings.DigitalDocumentConnectionString)


        'Dim ds1 As New DataSet
        '    adapter1.Fill(ds1, "Layer")


        '    ds1.Tables("Template").Rows(0)(0).ToString()





        '    End Using
        '    Catch ex As Exception
        'MessageBox.Show(ex.Message)
        '    End Try

        '  End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Context.Text = Context.Text & "<h1></h1>"

    End Sub

    Private Sub h2B_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles h2B.Click
        Context.Text = Context.Text & "<h2></h2>"
    End Sub

    Private Sub h3B_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles h3B.Click
        Context.Text = Context.Text & "<h3></h3>"
    End Sub

    Private Sub h4B_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles h4B.Click
        Context.Text = Context.Text & "<h4></h4>"
    End Sub

    Private Sub CenterB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles paragrah.Click
        Context.Text = Context.Text & "<p></p>"

    End Sub

    Private Sub brB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles brB.Click
        'Context.Text = Context.Text & "<br />"
        MsgBox(WebBrowser2.DocumentText.Length)
    End Sub

    Private Sub UB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UB.Click
        Context.Text = Context.Text & "<u></u>"
    End Sub

    Private Sub BB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BB.Click
        Context.Text = Context.Text & "<strong></strong>"
    End Sub

    Private Sub ExportToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportToolStripMenuItem.Click

    End Sub

    Private Sub TestToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TestToolStripMenuItem1.Click
        main.Show()

    End Sub

    Private Sub WebBrowser2_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser2.DocumentCompleted

        If isOpen Then
            Dim text As HtmlDocument = WebBrowser2.Document

            Dim sql As String = "SELECT * FROM Content WHERE Did =" + Did.ToString
            Try
                Using cnn As New OleDbConnection(My.Settings.DigitalDocumentConnectionString)

                    Using cmd As New OleDbCommand(sql, cnn)
                        cnn.Open()

                        Using dr As OleDbDataReader = cmd.ExecuteReader()


                            While dr.Read
                                text.GetElementById(dr("Lid")).SetAttribute("innerHTML", dr("InnerHtml1") + "" + dr("InnerHtml2"))


                            End While
                        End Using
                    End Using





                End Using
            Catch ex As Exception
                MessageBox.Show(ex.Message)

            End Try

            Context.Text = text.GetElementById(LayerComboBox.SelectedValue).GetAttribute("innerHTML")


        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (LayerComboBox.Text = "") Then
            MsgBox("Διάλεξαι Layer")
        Else
            If (Context.Text = "") Then
                MsgBox("Συμπλήρωσε το πεδίο")
            Else
                Dim x As Integer = 0
                Dim idl As String
                Dim layer As Integer
                Dim text As HtmlDocument = WebBrowser2.Document
                Dim content As String
                Dim layertitle As String
                layer = LayerComboBox.SelectedValue

                content = text.GetElementById(layer).GetAttribute("innerHTML")


                dcol(LayerComboBox.SelectedIndex) = dcol(LayerComboBox.SelectedIndex) + 1

                'text.GetElementById(layer).SetAttribute("innerHTML", content + "<div id='col" + CStr(dcol(LayerComboBox.SelectedIndex)) + "'>" + Context.Text + "</div>")


                text.GetElementById(layer).SetAttribute("innerHTML", Context.Text)




                content = text.GetElementById(layer).GetAttribute("innerHTML")


                Dim w As String = ""
                Dim y As String = ""
                If (content.Length > 254) Then
                    w = content.Substring(0, 254)
                    y = content.Substring(254)

                    '  MsgBox(w)
                    ' MsgBox(y)
                Else
                    w = content

                End If


                '  Dim strSQL As String = "UPDATE Content SET InnerHtml1='" + w.ToString + "', InnerHtm2='" + y.ToString + "' WHERE Did=" + Did.ToString + " AND Lid=" + layer.ToString
                Dim sql As String = "SELECT * FROM Content WHERE Did =" + Did.ToString + " AND Lid =" + layer.ToString


                Try
                    Using cnn As New OleDbConnection(My.Settings.DigitalDocumentConnectionString)

                        

                        Using cmd As New OleDbCommand(sql, cnn)
                            cnn.Open()

                            Using dr As OleDbDataReader = cmd.ExecuteReader()


                                While dr.Read
                                    Me.ContentTableAdapter1.Update(Did, layer, w, y, dr("Cid"), Did, layer, dr("InnerHtml1"), dr("InnerHtml2"))
                                End While
                            End Using
                        End Using





                    End Using
                Catch ex As Exception
                    MessageBox.Show(ex.Message)

                End Try


                '  Dim strSQL1 As String = ""
                'Dim sqlConnection1 As New System.Data.SqlClient.SqlConnection(My.DigitalDocumentConnectionString)

                ' cmd.CommandText = 




                ' Me.ContentTableAdapter1.Update(



                ' If (LayerComboBox.Text = 1) Then
                'text.GetElementById("Title").SetAttribute("innerHTML", Context.Text)
                ' text.GetElementById("Title").Style = "position: absolute; top:0px; left:40%;overflow:auto;"
                ' ElseIf (LayerComboBox.Text = 2) Then
                ' layer = "LeftCol"
                ' l1 = l1 + 1
                ' x = l1
                ' ElseIf (LayerComboBox.Text = 3) Then
                ' If LayerComboBox.Items.Count = 4 Then
                'layer = "CenterCol"
                ' l2 = l2 + 1
                ' x = l2
                ' Else
                '  layer = "RightCol"
                '  l3 = l3 + 1
                '  x = l3
                ' End If
                '   ElseIf (LayerComboBox.Text = 4) Then
                '   layer = "RightCol"
                '   l3 = l3 + 1
                '   x = l3
                ' End If


                'Dim content As String



                ' If (LayerComboBox.Text > 1) Then
                '
                ' idl = layer + x.ToString
                ' text.GetElementById(layer).SetAttribute("innerHTML", content + "<div id='" + idl + "'>" + Context.Text + "</div>")
                '  Context.Text = ""
                '  End If

            End If
        End If

    End Sub

    Private Sub LayerComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LayerComboBox.SelectedIndexChanged
        'MsgBox(LayerComboBox.Text + " and " + LayerComboBox.SelectedValue.ToString)

        Dim text As HtmlDocument = WebBrowser2.Document

        If flag Then

            Context.Text = text.GetElementById(LayerComboBox.SelectedValue).GetAttribute("innerHTML")



        End If


        '  

    End Sub




    Private Sub a(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)

    End Sub

    Private Sub TabControl1_Selected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected



        Dim strSQL2 As String = "SELECT * FROM Layer WHERE TemplateId =" & Me.Form1TempText.Text & " OR TemplateId =0"

        Dim layernumber As Integer
        Dim html As String
        Dim content As String = ""

        Dim text As HtmlDocument = WebBrowser2.Document
        html = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN''http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>"
        html += "<html xmlns='http://www.w3.org/1999/xhtml'><head><meta content='text/html; charset=utf-8' http-equiv='Content-Type' /><title id='toptitle'></title></head><body >"
        html += "<div style='position: absolute; width: 100%; height: 100%; z-index: 1' id='Main'><h1><center><div style=' position: absolute; top:0px; left:40%; width: 300px; height: 20%;  overflow-x: hidden;	word-wrap: break-word ; z-index: 1' id='9'>"
        Dim sql As String = "SELECT * FROM Content WHERE Did =" + Did.ToString + " AND Lid =9"


        Try
            Using cnn As New OleDbConnection(My.Settings.DigitalDocumentConnectionString)



                Using cmd As New OleDbCommand(sql, cnn)
                    cnn.Open()

                    Using dr As OleDbDataReader = cmd.ExecuteReader()


                        While dr.Read
                            html += dr("InnerHtml1") + "" + dr("InnerHtml2")
                        End While
                    End Using
                End Using





            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try



        html += "</div></center></h1><div style=' position: absolute; width: 100%; top:25%; height:75%; z-index: 1; left: 0px;' id='Core'>"
     


        Dim strSQL As String = "SELECT count(*) as cc FROM Layer WHERE TemplateId =" & Me.Form1TempText.Text
        Dim strSQL1 As String = "SELECT  Layer.*,Content.InnerHtml1,Content.InnerHtml2 FROM (Document INNER JOIN Content ON Document.DId = Content.Did) INNER JOIN Layer ON Content.Lid = Layer.Lid WHERE Content.Did=" + Did.ToString

        Try
            Using cnn As New OleDbConnection(My.Settings.DigitalDocumentConnectionString)
                Using cmd As New OleDbCommand(strSQL, cnn)
                    cnn.Open()

                    Using dr As OleDbDataReader = cmd.ExecuteReader()


                        While dr.Read
                            layernumber = dr("cc")
                        End While
                        cnn.Close()

                    End Using
                End Using
            End Using

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            html = "Problem"
        End Try


        Try
            Using cnn As New OleDbConnection(My.Settings.DigitalDocumentConnectionString)

                Dim layer(0 To layernumber) As String
                Dim counter As Integer = 0

                Using cmd As New OleDbCommand(strSQL1, cnn)
                    cnn.Open()

                    Using dr As OleDbDataReader = cmd.ExecuteReader()


                        While dr.Read
                         
                            layer(counter) = "<div id='" + CStr(dr("Lid")) + "' style='background-color:gray; position:" + dr("Position") + "; top:" + dr("PossitionY") + "; left:" + dr("PossitionX") + "; width:" + dr("DissmentionX") + ";  " + dr("InnerLayer") + "'>" + dr("InnerHtml1") + "" + dr("InnerHtml2") + "</div>" '<input type='hidden' id='flag' value='0'>

                            counter = counter + 1
                        End While
                    End Using
                End Using

                Dim intCount As Integer

                For intCount = 0 To layernumber
                    content += layer(intCount)

                Next intCount



            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            html = "Problem"
        End Try





        html += content
        html += "</div></div></body></html>"


        WebBrowser3.DocumentText = html






    End Sub

    Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click
        DeleteDocument.Show()
    End Sub

    Private Sub Form1TempText_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form1TempText.TextChanged
        Dim strSQL1 As String = "SELECT Title FROM Document WHERE DId =" & Did.ToString


        Try
            Using cnn As New OleDbConnection(My.Settings.DigitalDocumentConnectionString)
                Using cmd As New OleDbCommand(strSQL1, cnn)
                    cnn.Open()

                    Using dr As OleDbDataReader = cmd.ExecuteReader()


                        While dr.Read
                            Form1DocText.Text = dr("Title")
                        End While
                        cnn.Close()

                    End Using
                End Using
            End Using

        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try
    End Sub

    Private Sub NewToolStripMenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem4.Click
        Form5.Show()

    End Sub

    Private Sub ChangeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChangeToolStripMenuItem.Click
        Form3.Show()
    End Sub

    Private Sub DeleteToolStripMenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem3.Click
        Form4.Show()
    End Sub
End Class
